<?php
//adminModel
class AdminModel extends Model {

	//验证用户名和密码
	public function checkUser($username,$password){
		$password = $password;
		$where = "name = '$username' AND pwd = '$password'";
		// echo $sql;
		// exit;
		return $this->total($where);
		
	}


	//获取所有的管理员
	public function getAdmins(){
		$sql = "SELECT * FROM view_userprivilege";
		return $this->db->getAll($sql);
	}
	
	//更改管理员密码
	public function changePwd($data){
		$sql="update {$this->table} set pwd='{$data['pwd']}' where name='{$data['name']}'";
		return $this->db->query($sql);
	}
	
	//
	public function setAdmin($user,$authority){
		$data=$authority;
		$this->db->query("begin");
		if ($data['id']=$this->insert($user)) {
			$sql="insert into tb_purview (id,sysset,readerset,bookset,borrowback,sysquery) 
					values({$data['id']},{$data['sysset']},{$data['readerset']},
					{$data['bookset']},{$data['borrowback']},{$data['sysquery']})";
			if ($this->db->query($sql)) {
				$this->db->query("COMMIT");
				return true;
			}else {
				$this->db->query("ROLLBACK");
				return false;
			}
		}else {
			return false;
		}
	}
	
	public function updateAdmin($user,$authority){
		$data=$authority;
		$this->db->query("begin");
		
		$sql="select * from view_userprivilege where name='{$user['name']}'";
		if ($this->db->query($sql)) {
			$sql1="update tb_purview set sysset={$data['sysset']},readerset={$data['readerset']},
			bookset={$data['bookset']},borrowback={$data['borrowback']},sysquery={$data['sysquery']} 
			where id={$data['id']}";
			if ($this->db->query($sql1)) {
				$this->db->query("COMMIT");
				return true;
			}else {
				$this->db->query("ROLLBACK");
				return false;
			}
		}else{
			if ($this->update($user)) {
				$sql1="update tb_purview set sysset={$data['sysset']},readerset={$data['readerset']},
				bookset={$data['bookset']},borrowback={$data['borrowback']},sysquery={$data['sysquery']}
				where id={$data['id']}";
				if ($this->db->query($sql1)) {
					$this->db->query("COMMIT");
					return true;
				}else {
					$this->db->query("ROLLBACK");
					return false;
				}
			}else {
				return false;
			}
		}
	}
	
	//获取一个管理员,以二维数组形式返回
	public function getOneAdmin($id){
		$sql = "SELECT * FROM view_userprivilege where id=$id";
		return $this->db->getRow($sql);
	}

}